package com.thinkpadteam.edugra;

import java.util.ArrayList;

import com.thinkpadteam.edugra.R;
import com.thinkpadteam.edugra.fragmentmakan.MainMakanFragment;
import com.thinkpadteam.edugra.fragmentmandi.MainMandiFragment;
import com.thinkpadteam.edugra.fragmentsekolah.MainSekolahFragment;
import com.thinkpadteam.edugra.gridview.GridViewAdapter;
import com.thinkpadteam.edugra.gridview.ImageItem;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainLessonActivity extends Activity {
	private GridView gridView;
	private GridViewAdapter customGridAdapter;
	private String[] imgName;
	private MediaPlayer player;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_lesson_activity);
		imgName=getResources().getStringArray(R.array.image_name);
		gridView = (GridView) findViewById(R.id.gridView);
		
/* memasukkan gambar dan keterangannya ke dalam gridview layout */		
		customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
		gridView.setAdapter(customGridAdapter);

/* action listener untuk masing2 gridview */		
		gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				openactivityclass(position);
			}

		});
		
/* to mention the name of the activity by playing mp3 or raw */		
		
	    gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
	        @Override
	        public boolean onItemLongClick(AdapterView<?> parent, View v,
	                int position, long id) {
				playSound(position);
	            return false;
	        }
	    });

	}

	private ArrayList<ImageItem> getData() {
		final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
		// retrieve String drawable array
		TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
		for (int i = 0; i < imgs.length(); i++) {
			Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
					imgs.getResourceId(i, -1));
			imageItems.add(new ImageItem(bitmap, imgName[i]));
		}
		return imageItems;
	}


	public void openactivityclass(int arg){
		Intent intent = null;
		switch (arg) {
		case 0:
			intent = new Intent(MainLessonActivity.this, MainMakanFragment.class);
			intent.putExtra("materi", "makan");
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
			break;
		case 1: 
			intent = new Intent(MainLessonActivity.this, MainMandiFragment.class);
			intent.putExtra("materi", "mandi");
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
			break;
		case 2: 
			intent = new Intent(MainLessonActivity.this, MainSekolahFragment.class);
			intent.putExtra("materi", "sekolah");
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
			break;			
		default:
			break;
		}
			
			
	}
	
	private void playSound(int arg){
		try{
			if (player.isPlaying()) {
				player.stop();
				player.release();
			}
		}catch(Exception e){
		}
		switch(arg) {
		case 0 : 
			player = MediaPlayer.create(this, R.raw.makan);
			break;
		case 1 :
			player = MediaPlayer.create(this, R.raw.minum);
			break;
		case 2 :
			player = MediaPlayer.create(this, R.raw.memasak);
			break;
		case 3 :
			player = MediaPlayer.create(this, R.raw.berkebun);
			break;
		default :
			break;
		}
		player.setLooping(false); // Set looping
		player.start();
	}		

}
